package com.codedancer.campus.po;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.codedancer.campus.common.typehandler.CodeEnumTypeHandler;
import com.codedancer.campus.enums.LostFoundItemStatus;
import com.codedancer.campus.enums.LostFoundItemType;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.gitee.sunchenbin.mybatis.actable.annotation.Column;
import com.gitee.sunchenbin.mybatis.actable.annotation.Table;
import com.gitee.sunchenbin.mybatis.actable.constants.MySqlTypeConstant;
import java.io.Serializable;
import java.util.Date;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@TableName(value = "lost_found_item", autoResultMap = true)
@Table(name = "lost_found_item", comment = "失物招领物品")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class LostFoundItem extends Model<LostFoundItem> implements Serializable {

    @TableId
    @TableField("uuid")
    @Column(name = "uuid", type = MySqlTypeConstant.VARCHAR, length = 36, isKey = true, isNull = false, comment = "uuid")
    private String uuid;

    @TableField("name")
    @Column(name = "name", type = MySqlTypeConstant.VARCHAR, length = 16, isNull = false, comment = "物品名称")
    private String name;

    @TableField("location")
    @Column(name = "location", type = MySqlTypeConstant.VARCHAR, length = 36, isNull = false, comment = "丢失或拾取地点")
    private String location;

    @TableField("description")
    @Column(name = "description", type = MySqlTypeConstant.TEXT, isNull = false, comment = "物品描述")
    private String description;

    @TableField("publish_user_uuid")
    @Column(name = "publish_user_uuid", type = MySqlTypeConstant.VARCHAR, length = 36, isNull = false, comment = "发布人uuid")
    private String publishUserUuid;

    @TableField("publish_school_uuid")
    @Column(name = "publish_school_uuid", type = MySqlTypeConstant.VARCHAR, length = 36, isNull = false, comment = "发布学校uuid")
    private String publishSchoolUuid;

    @TableField("back_reason")
    @Column(name = "back_reason", type = MySqlTypeConstant.TEXT, isNull = true, comment = "退回原因")
    private String backReason;

    @TableField(value = "type", typeHandler = CodeEnumTypeHandler.class)
    @Column(name = "type", type = MySqlTypeConstant.TINYINT, length = 1, isNull = false, comment = "物品类型 1表示拾取 2表示丢失")
    private LostFoundItemType type;

    @TableField(value = "status", typeHandler = CodeEnumTypeHandler.class)
    @Column(name = "status", type = MySqlTypeConstant.TINYINT, length = 1, isNull = false, comment = "物品状态 1表示待审核 2未通过 3表示未认领 4表示已认领 5表示未找到 6表示已找到 7表示已撤回")
    private LostFoundItemStatus status;

    @TableField(value = "contact_way")
    @Column(name = "contact_way", type = MySqlTypeConstant.VARCHAR, length = 36, isNull = false, comment = "联系方式")
    private String contactWay;

    @TableField("deleted")
    @Column(name = "deleted", type = MySqlTypeConstant.TINYINT, length = 1, isNull = false, comment = "是否删除 1表示删除 0表示未删除", defaultValue = "0")
    private Short deleted;

    @TableField("view_number")
    @Column(name = "view_number", type = MySqlTypeConstant.INT, length = 10, isNull = false, comment = "浏览量", defaultValue = "0")
    private Integer viewNumber;

    @TableField("category_uuid")
    @Column(name = "category_uuid", type = MySqlTypeConstant.VARCHAR, length = 36, isNull = false, comment = "分类uuid")
    private String categoryUuid;

    @TableField("plate_uuid")
    @Column(name = "plate_uuid", type = MySqlTypeConstant.VARCHAR, length = 36, isNull = false, comment = "板块uuid")
    private String plateUuid;

    @Column(name = "create_time", type = MySqlTypeConstant.DATETIME, isNull = false, comment = "创建时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    @Column(name = "update_time", type = MySqlTypeConstant.DATETIME, isNull = false, comment = "更新时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    @TableField("examine_count")
    @Column(name = "examine_count", type = MySqlTypeConstant.INT, length = 1, isNull = false, comment = "审核次数，最多审核五次", defaultValue = "0")
    private Integer examineCount;

    @TableField("is_Admin_revoke")
    @Column(name = "is_Admin_revoke", type = MySqlTypeConstant.BIT, length = 1, isNull = false, comment = "是否是管理员撤回", defaultValue = "false")
    private Boolean isAdminRevoke;

    @TableField("is_sync_liberty_square")
    @Column(name = "is_sync_liberty_square", type = MySqlTypeConstant.BIT, length = 1, isNull = false, comment = "是否同步到自由广场", defaultValue = "false")
    private Boolean isSyncLibertySquare;

    // 关联发布人
    @TableField(exist = false)
    private User publishUser;

    // 关联发布学校
    @TableField(exist = false)
    private School publishSchool;

    // 关联分类
    @TableField(exist = false)
    private Category category;

    /**
     * 是否能同步到自由广场或者取消同步
     * 1.被管理员撤回不能同步
     * 2.状态处于待审核、未通过、已撤回、已找到、已认领不能同步
     *
     * @return
     */
    public boolean isCanSyncLibertySquareOrCancel() {
        if (isAdminRevoke) {
            return false;
        }
        if (status.equals(LostFoundItemStatus.PENDING_REVIEW) ||
            status.equals(LostFoundItemStatus.NOT_PASS) ||
            status.equals(LostFoundItemStatus.RECALL) ||
            status.equals(LostFoundItemStatus.FOUNDED) ||
            status.equals(LostFoundItemStatus.CLAIMED)) {
            return false;
        }
        return true;
    }

    /**
     * 是否能撤回物品
     * 1.被管理员撤回后不能撤回
     * 2.状态处于待审核、未通过、已找到、已撤回、已认领不能撤回
     * @return
     */
    public boolean isCanRevoke(){
        if (isAdminRevoke){
            return false;
        }
        if (status.equals(LostFoundItemStatus.PENDING_REVIEW) ||
            status.equals(LostFoundItemStatus.NOT_PASS) ||
            status.equals(LostFoundItemStatus.RECALL) ||
            status.equals(LostFoundItemStatus.FOUNDED) ||
            status.equals(LostFoundItemStatus.CLAIMED)) {
            return false;
        }
        return true;
    }
}
